﻿Imports System.Windows.Forms

Public Class DatVe_ChuyenBay

    Private f As DatVe

    Public Sub New(ByRef f As DatVe)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.f = f
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If dgv_danhsachchuyenbay.SelectedRows.Count <> 0 Then
            Dim table As DataTable = dgv_danhsachchuyenbay.DataSource
            Dim id As String = table.Rows.Item(dgv_danhsachchuyenbay.SelectedRows.Item(0).Index).Item(0).ToString
            Me.f.tb_machuyenbay.Text = id
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Else
            MessageBox.Show("Chưa chọn chuyến bay!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DatVe_ChuyenBay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim bus_sanbay As New BUS.SanBay
        Dim bus_chuyenbay As New BUS.ChuyenBay

        cbb_sanbaydi.DisplayMember = "ten"
        cbb_sanbaydi.ValueMember = "id"
        cbb_sanbaydi.DataSource = bus_sanbay.lay_dataset_san_bay.Tables(0)
        cbb_sanbaydi.SelectedIndex = -1

        cbb_sanbayden.DisplayMember = "ten"
        cbb_sanbayden.ValueMember = "id"
        cbb_sanbayden.DataSource = bus_sanbay.lay_dataset_san_bay.Tables(0)
        cbb_sanbayden.SelectedIndex = -1

        cbb_machuyenbay.DisplayMember = "id"
        cbb_machuyenbay.ValueMember = "id"
        cbb_machuyenbay.DataSource = bus_chuyenbay.lay_dataset_chuyen_bay.Tables(0)
        cbb_machuyenbay.SelectedIndex = -1

    End Sub

    Private Sub cbb_machuyenbay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbb_machuyenbay.SelectedIndexChanged, cbb_sanbayden.SelectedIndexChanged, cbb_sanbaydi.SelectedIndexChanged, dtp_ngaybay.ValueChanged, cbb_machuyenbay.TextChanged, cbb_sanbayden.TextChanged, cbb_sanbaydi.TextChanged
        Dim bus_chuyenbay As New BUS.ChuyenBay
        Dim machuyenbay As String = ""
        If Not IsNothing(cbb_machuyenbay.SelectedValue) Then
            machuyenbay = cbb_machuyenbay.SelectedValue
        End If
        Dim sanbaydi As String = ""
        If Not IsNothing(cbb_sanbaydi.SelectedValue) Then
            sanbaydi = cbb_sanbaydi.SelectedValue
        End If
        Dim sanbayden As String = ""
        If Not IsNothing(cbb_sanbayden.SelectedValue) Then
            sanbayden = cbb_sanbayden.SelectedValue
        End If
        Dim ngaybay As String = ""
        If Not IsNothing(dtp_ngaybay.Text) Then
            ngaybay = dtp_ngaybay.Text
        End If

        Dim dataset = bus_chuyenbay.lay_dataset_chuyen_bay(machuyenbay, sanbaydi, sanbayden, ngaybay)
        If dataset.Tables(0).Rows.Count = 0 Then
            dgv_danhsachchuyenbay.DataSource = Nothing
        Else
            dgv_danhsachchuyenbay.DataSource = dataset.Tables(0)
        End If
    End Sub

    Private Sub dgv_danhsachchuyenbay_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_danhsachchuyenbay.CellDoubleClick
        Dim table As DataTable = dgv_danhsachchuyenbay.DataSource
        Dim id As String = table.Rows.Item(e.RowIndex).Item(0).ToString
        Me.f.tb_machuyenbay.text = id
        Me.Close()
    End Sub

End Class
